<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <div class="fixed-table-actionbar">
        <TableFilter v-model="search" />
        <ActionBar v-role="['worker','workerGroupLeader','backup', 'zongjian']" :search="search" />
      </div>
      <PageableTable
        :loading="loading"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        :multiple-selected="false"
        class="fixed-table-table"
        @page-change="onPageChange"
        @sort-change="onSortChange"
      >
        <el-table-column v-for="column in insideColumns" :key="column.key" :width="column.width" :label="column.title" :prop="column.key" :sortable="column.sortable">
          <template slot-scope="scope">
            <column-render :render="column.render" :row="scope.row" :column="column" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button v-role="['worker']" type="text" title="编辑" size="mini" icon="el-icon-edit" @click="edit(row.id)" />
            <el-button v-role="['worker']" type="text" title="删除" size="mini" icon="el-icon-delete" @click="remove(row.id)" />
            <el-button type="text" title="查看" size="mini" icon="el-icon-notebook-2" @click="view(row.id)" />
          </template>
        </el-table-column>
      </PageableTable>
    </div>
    <ViewDialog title="查看" class="fixed-dialog-height" :show.sync="dialogViewVisible" width="60%" @on-cancel="viewCancel">
      <ViewForm :id="editId" />
    </ViewDialog>
    <FormDialog title="编辑" class="fixed-dialog-height" :show.sync="dialogFormVisible" width="60%" @on-ok="ok" @on-cancel="cancel">
      <EditForm :id="editId" ref="editForm" />
    </FormDialog>
  </div>
</template>

<script>
// secondary package based on el-pagination
import ViewDialog from '@neu/biz-core/components/ViewDialog'
import FormDialog from '@neu/biz-core/components/FormDialog'
// 表格查询组件独立编写，需要传递pageable-table实例的mixin方法filter
import TableFilter from './components/tableFilter'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import ViewForm from './components/viewForm'
import EditForm from './components/editForm'
import ActionBar from './components/actionBar'
import PageableTable from '@neu/biz-core/components/PageableTable'
import databind from '@neu/biz-core/mixins/databind'
import { page, remove } from '@neu/dcom-ui/api/operationSupportWeekly'
import moment from 'moment'
import { formatDate } from '@neu/biz-core/utils/utils'
export default {
  name: 'OperationSupportWeeklyPage',
  components: {
    TableFilter,
    FormDialog,
    EditForm,
    ActionBar,
    PageableTable,
    ViewDialog,
    ViewForm
  },
  // 加入混入，使表格混入更多的api操作
  mixins: [databind],
  data() {
    return {
      r: page,
      d: remove,
      insideColumns: [
        {
          title: '周',
          key: 'weekDate',
          sortable: 'custom',
          width: '120',
          render: (h, params) => {
            return h('span', moment(params.row.weekDate).year() + ' 年 ' + moment(params.row.weekDate).week() + ' 周')
          }
        },
        { title: '单位名称', key: 'companyName', sortable: 'custom', width: '180' },
        { title: '服务项目', key: 'serviceItem', sortable: 'custom', width: '180' },
        { title: '实际到场人数', key: 'actualAttendanceNumber', sortable: 'custom', width: '120' },
        { title: '本周工作情况', key: 'weekWorkInfo', sortable: 'custom' },
        { title: '总结描述', key: 'summaryDesc', sortable: 'custom' },
        { title: '下周工作计划', key: 'nextWeekWorkPlan', sortable: 'custom' },
        { title: '创建时间', key: 'createTime',
          render: (h, params) => { return h('span', formatDate(params.row.createTime, 'YYYY-MM-DD HH:mm')) }
        },
        { title: '备注', key: 'remark', sortable: 'custom' }
      ],
      dialogFormVisible: false,
      dialogViewVisible: false,
      editId: -1,
      search: {}
    }
  },
  methods: {
    view(id) {
      // 打开对话框
      this.editId = id
      this.dialogViewVisible = true
    },
    // 表格编辑按钮点击事件处理
    edit(id) {
      // 打开对话框
      this.editId = id
      this.dialogFormVisible = true
      // 更新当前被编辑实体的ID，自动传递至编辑表单
    },
    // 编辑资产对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
      })
    },
    cancel() {
      this.editId = -1
      this.$refs.editForm.reset()
      // 关闭对话框
      this.dialogFormVisible = false
    },
    viewCancel() {
      this.editId = -1
      // 关闭对话框
      this.dialogViewVisible = false
    }
  }
}
</script>
